#include "ConfigApp.h"
#include "WirelessProtocols/Console.h"
#include "Transceivers/Transceivers.h"
#include "WirelessProtocols/SymbolTime.h"
#include "Transceivers/Security.h"
#include "WirelessProtocols/MCHP_API.h"

#include "TratamentoRTC.h"
#include <i2c.h>

BOOL gravaHorario(BYTE *horario) {
    if (!getSemaforoI2C()) {
        setSemaforoI2C(TRUE);
        /* Baud rate is set for 100 Khz */
        int config2 = 96;
        /* Configure I2C for 7 bit address mode */
        int config1 = (I2C1_ON & I2C1_IDLE_CON & I2C1_CLK_REL
                & I2C1_IPMI_DIS & I2C1_7BIT_ADD
                & I2C1_SLW_DIS & I2C1_SM_DIS &
                I2C1_GCALL_DIS & I2C1_STR_DIS &
                I2C1_ACK & I2C1_ACK_DIS & I2C1_RCV_DIS &
                I2C1_STOP_DIS & I2C1_RESTART_DIS
                & I2C1_START_DIS);
        OpenI2C1(config1, config2);
        IdleI2C1();
        StartI2C1();
        while (I2C1CONbits.SEN);
        MasterWriteI2C1(0xD0);
        while (I2C1STATbits.TBF);
        while (I2C1STATbits.ACKSTAT);
        MasterWriteI2C1(0x00);
        while (I2C1STATbits.TBF);
        while (I2C1STATbits.ACKSTAT);
        int aux;
        for (aux = 0; aux < 7; aux++) {
            MasterWriteI2C1(*horario);
            while (I2C1STATbits.TBF);
            while (I2C1STATbits.ACKSTAT);
            horario++;
        }
        IdleI2C1();
        StopI2C1();
        while (I2C1CONbits.PEN);
        CloseI2C1();
        setSemaforoI2C(FALSE);
        return TRUE;
    } else {
        return FALSE;
    }
}

BOOL leHorario(int *vetorHorario) {
    if (!getSemaforoI2C()) {
        int config2 = 96;
        int config1 = (I2C1_ON & I2C1_IDLE_CON & I2C1_CLK_REL
                & I2C1_IPMI_DIS & I2C1_7BIT_ADD
                & I2C1_SLW_DIS & I2C1_SM_DIS &
                I2C1_GCALL_DIS & I2C1_STR_DIS &
                I2C1_ACK & I2C1_ACK_DIS & I2C1_RCV_DIS &
                I2C1_STOP_DIS & I2C1_RESTART_DIS
                & I2C1_START_DIS);
        setSemaforoI2C(TRUE);
        OpenI2C1(config1, config2);
        IdleI2C1();
        StartI2C1();
        while (I2C1CONbits.SEN);
        MasterWriteI2C1(0xD0);
        while (I2C1STATbits.TBF);
        while (I2C1STATbits.ACKSTAT);
        MasterWriteI2C1(0x00);
        while (I2C1STATbits.TBF);
        while (I2C1STATbits.ACKSTAT);
        IdleI2C1();
        RestartI2C1();
        while (I2C1CONbits.RSEN);
        MasterWriteI2C1(0xD1);
        while (I2C1STATbits.TBF);
        while (I2C1STATbits.ACKSTAT);
        char aux1;
        aux1 = MasterReadI2C1();
        while (I2C1STATbits.RBF);
        *vetorHorario = aux1;
        vetorHorario++;
        AckI2C1();
        while (I2C1CONbits.ACKEN);
        char aux2;
        aux2 = MasterReadI2C1();
        while (I2C1STATbits.RBF);
        *vetorHorario = aux2;
        vetorHorario++;
        AckI2C1();
        while (I2C1CONbits.ACKEN);
        char aux3;
        aux3 = MasterReadI2C1();
        while (I2C1STATbits.RBF);
        *vetorHorario = aux3;
        vetorHorario++;
        AckI2C1();
        while (I2C1CONbits.ACKEN);
        char aux4;
        aux4 = MasterReadI2C1();
        while (I2C1STATbits.RBF);
        *vetorHorario = aux4;
        vetorHorario++;
        AckI2C1();
        while (I2C1CONbits.ACKEN);
        char aux5;
        aux5 = MasterReadI2C1();
        while (I2C1STATbits.RBF);
        *vetorHorario = aux5;
        vetorHorario++;
        AckI2C1();
        while (I2C1CONbits.ACKEN);
        char aux6;
        aux6 = MasterReadI2C1();
        while (I2C1STATbits.RBF);
        *vetorHorario = aux6;
        vetorHorario++;
        AckI2C1();
        while (I2C1CONbits.ACKEN);
        char aux7;
        aux7 = MasterReadI2C1();
        while (I2C1STATbits.RBF);
        *vetorHorario = aux7;
        vetorHorario++;
        NotAckI2C1();
        while (I2C1CONbits.ACKEN);
        IdleI2C1();
        StopI2C1();
        while (I2C1CONbits.PEN);
        CloseI2C1();
        setSemaforoI2C(FALSE);
        return TRUE;
    } else {
        return FALSE;
    }
}

void iniciaRTC(void) {
        if (!getSemaforoI2C()) {
        setSemaforoI2C(TRUE);
        /* Baud rate is set for 100 Khz */
        int config2 = 96;
        /* Configure I2C for 7 bit address mode */
        int config1 = (I2C1_ON & I2C1_IDLE_CON & I2C1_CLK_REL
                & I2C1_IPMI_DIS & I2C1_7BIT_ADD
                & I2C1_SLW_DIS & I2C1_SM_DIS &
                I2C1_GCALL_DIS & I2C1_STR_DIS &
                I2C1_ACK & I2C1_ACK_DIS & I2C1_RCV_DIS &
                I2C1_STOP_DIS & I2C1_RESTART_DIS
                & I2C1_START_DIS);
        OpenI2C1(config1, config2);
        IdleI2C1();
        StartI2C1();
        while (I2C1CONbits.SEN);
        MasterWriteI2C1(0xD0);
        while (I2C1STATbits.TBF);
        while (I2C1STATbits.ACKSTAT);
        MasterWriteI2C1(0x00);
        while (I2C1STATbits.TBF);
        while (I2C1STATbits.ACKSTAT);
        int aux;
        for (aux = 0; aux < 7; aux++) {
            MasterWriteI2C1(0x00);
            while (I2C1STATbits.TBF);
            while (I2C1STATbits.ACKSTAT);
        }
        MasterWriteI2C1(0x10);
        while (I2C1STATbits.TBF);
        while (I2C1STATbits.ACKSTAT);
        IdleI2C1();
        StopI2C1();
        while (I2C1CONbits.PEN);
        CloseI2C1();
        setSemaforoI2C(FALSE);
    }
}

